<?php
/**
 * Created by PhpStorm.
 * User: vinhtd
 * Date: 3/19/14
 * Time: 5:54 PM
 */
libxml_disable_entity_loader(false);
function fetch_vocab($refresh = false) {
//    global $loading;
//    $loading = 'Load on remote site';
//    $memcache = new Memcache();
    $doc = new DOMDocument();
//    if ($refresh) {
        $page_content = load_remote_page_content();
//    } else {
//        $page_content = $memcache->get('c2014');
//        if ($page_content) {
//            $loading = 'Load on cache';
//        } else {
//            $page_content = load_remote_page_content();
//            $refresh = true;
//        }
//    }
    if ($doc->loadHTML($page_content)) {
        // Save to cache
//        if ($refresh) $memcache->set('c2014', $page_content);

        $xpath = new DOMXPath($doc);
        $content_root = $xpath->query("//td[contains(normalize-space(@class), 'sites-layout-tile')]/div[contains(normalize-space(@dir), 'ltr')]");
        if ($content_root->length > 0) {
            $content_root = $content_root->item(0);
            $items = $xpath->query(".//h1|.//h2|.//h3|.//h4|.//h5|.//h6|.//div", $content_root);
            $vocab = array();
            if ($items->length > 0) {
                $month = 0;
                foreach ($items as $item) {
                    if (preg_match('/^h\d$/', $item->tagName)) {
                        $month = $item->textContent;
                    } else {
                        $matched = preg_match('/^(\d+): (.*)/', $item->textContent, $matches);
                        if($matched) {
                            $vocab[$month][$matches[1]] = $matches[2];
                        }
                    }
                }
            }
            return $vocab;
        }
    }
    return false;
}

function load_remote_page_content() {
    global $year;
    $page_url = 'https://sites.google.com/site/thaidacvinh4chinese/sheng-ci/' . $year;
    $page_content = file_get_contents($page_url);
    $page_content = str_replace('<meta http-equiv="X-UA-Compatible" content="chrome=1" />', '<meta http-equiv="Content-type" content="text/html; charset=utf-8" />', $page_content);
    return $page_content;
}

function explore_day_words ($day_words) {
    $regex_p = '#/([^/]*)/#';
    if (preg_match_all($regex_p, $day_words ,$matches)) {
        $pinyin = $matches[1];
        $day_words = preg_replace($regex_p, '', $day_words);
        $regex = '#\((([^()]+|(?R))*)\)#';
        if (preg_match_all($regex, $day_words ,$matches)) {
            $day_words = preg_replace($regex, '', $day_words);
            $day_words = explode(', ', $day_words);
            foreach ($day_words as $key => $word) {
                unset($day_words[$key]);
                $word = preg_replace('/([^\/]*)\/.*/', '$1', $word);
                $day_words[$key] = array('word' => $word, 'pinyin' => $pinyin[$key], 'meaning' => $matches[1][$key], 'learned' => 0);
            }
            return $day_words;
        }
        return false;
    }
    return false;
}

function shuffle_assoc(&$array) {
    $keys = array_keys($array);

    shuffle($keys);

    foreach($keys as $key) {
        $new[$key] = $array[$key];
    }

    $array = $new;

    return true;
}